﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StockPredictor.Models;
using StockPredictor.Utilities;
using StockPredictor.App_Start;

namespace StockPredictor.Controllers
{
    public class MyPortfolioController : Controller
    {
        PortfolioRecordsContext portfolioContext = new PortfolioRecordsContext();
        UsersContext userContext = new UsersContext();

        #region Actions

        public ActionResult Index()
        {
            // get the portfolio records specific to this user:
            if (User.Identity.IsAuthenticated)
            {
                // information last updated
                ViewBag.CurrentTime = DateTime.Now;

                // get the portfolio information
                return View(PortfolioUtilities.GetPortfolioRecordsForUser(User.Identity.Name));
            }
            else
            {
                return RedirectToAction("Login", "Account");
            }
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(PortfolioRecordModel record)
        {
            // update the record information with user identifying information
            record.UserId = AccountUtilities.GetUserIdForUser(User.Identity.Name);

            portfolioContext.PortfolioRecords.Add(record);
            portfolioContext.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            var record = portfolioContext.PortfolioRecords.Find(id);
            if (record == null)
            {
                return HttpNotFound();
            }

            return View(record);
        }

        [HttpPost]
        public ActionResult Edit(PortfolioRecordModel record)
        {
            if (ModelState.IsValid)
            {
                portfolioContext.Entry(record).State = EntityState.Modified;
                portfolioContext.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(record);
        }

        public ActionResult Delete(int id)
        {
            var record = portfolioContext.PortfolioRecords.Find(id);
            if (record != null)
            {
                portfolioContext.PortfolioRecords.Remove(record);
                portfolioContext.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        #endregion Actions

        public JsonResult ChartResults(string stockSymbol)
        {
            var results = StockChartHelper.GetChartResults(stockSymbol);
            var jsonResult = Json(results.Select(x => new[] { x.x, x.open, x.high, x.low, x.close }));
            jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return jsonResult;
        }

        public static CurrentStock GetCurrentStockInfo(string stockSymbol,string userName)
        {
            return YahooFinance.GetCurrentStockData(stockSymbol,userName).FirstOrDefault();
        }
    }
}
